#include "js/json2.js"

#include "js/function.js"

var eraserSize = 8;

// 调节橡皮大小
function setEraserSize(size) {
    (function set_54512023925781() {
        try {
            var d = new ActionDescriptor();
            var r = new ActionReference();
            r.putEnumerated(stringIDToTypeID("brush"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
            d.putReference(stringIDToTypeID("null"), r);
            var d1 = new ActionDescriptor();
            d1.putUnitDouble(stringIDToTypeID("masterDiameter"), stringIDToTypeID("pixelsUnit"), size);
            d.putObject(stringIDToTypeID("to"), stringIDToTypeID("brush"), d1);
            executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
        } catch (e) {
            if (e.number != 8007) {
                alert("Line: " + e.line + "\n\n" + e, "Bug!", true);
                throw (e);
            }
        }
    })();
    eraserSize = size;
}

// 设置背景颜色
function setBackGroundColor(rgbData) {
    (function set_658935546875() //
        {
            try {
                var d = new ActionDescriptor();
                var r = new ActionReference();
                r.putProperty(stringIDToTypeID("color"), stringIDToTypeID("backgroundColor"));
                d.putReference(stringIDToTypeID("null"), r);
                var d1 = new ActionDescriptor();
                d1.putDouble(stringIDToTypeID("red"), rgbData.red);
                d1.putDouble(stringIDToTypeID("green"), rgbData.green);
                d1.putDouble(stringIDToTypeID("blue"), rgbData.blue);
                d.putObject(stringIDToTypeID("to"), stringIDToTypeID("RGBColor"), d1);
                d.putString(stringIDToTypeID("source"), "photoshopPicker");
                executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
            } catch (e) {
                if (e.number != 8007) {
                    alert("Line: " + e.line + "\n\n" + e, "Bug!", true);
                    throw (e);
                }
            }
        })();
}

// 选取仿制图章工具初始点
function setCloneStampPosition(x, y) {
    (function set_4842529296875() //打开
        {
            try {
                var d = new ActionDescriptor();
                var r = new ActionReference();
                r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("cloneSource"));
                r.putEnumerated(stringIDToTypeID("application"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
                d.putReference(stringIDToTypeID("null"), r);
                var d1 = new ActionDescriptor();
                var r1 = new ActionReference();
                r1.putName(stringIDToTypeID("layer"), "图层 0");
                d1.putReference(stringIDToTypeID("source"), r1);
                var d2 = new ActionDescriptor();
                d2.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), 300);
                d2.putUnitDouble(stringIDToTypeID("vertical"), stringIDToTypeID("pixelsUnit"), 24);
                d1.putObject(stringIDToTypeID("position"), stringIDToTypeID("point"), d2);
                d.putObject(stringIDToTypeID("to"), stringIDToTypeID("imageCachePreferences"), d1);
                executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
            } catch (e) {
                if (e.number != 8007) {
                    alert("Line: " + e.line + "\n\n" + e, "Bug!", true);
                    throw (e);
                }
            }
        }
    )();
}

// 获取颜色
function getDocColor(doc, x, y) {
    try {
        var samplers = doc.colorSamplers;
        if (samplers.length == 0) {
            var posA = new Array(x, y);
            samplers.add(posA);
        }
        var sam = doc.colorSamplers[0];
        var p = new Array(x, y)
        sam.move(p);
        var col = sam.color;
        return col.rgb;
    } catch (e) {
        return "000000"
    }
}

function eraseLine(document, x1, x2, y) {
    var lineArray = new Array()
    lineArray[0] = new PathPointInfo
    lineArray[0].kind = PointKind.CORNERPOINT
    lineArray[0].anchor = Array(x1, y)

    lineArray[0].leftDirection = lineArray[0].anchor
    lineArray[0].rightDirection = lineArray[0].anchor
    lineArray[1] = new PathPointInfo
    lineArray[1].kind = PointKind.CORNERPOINT
    lineArray[1].anchor = Array(x2, y)
    lineArray[1].leftDirection = lineArray[1].anchor
    lineArray[1].rightDirection = lineArray[1].anchor
    var lineSubPathArray = new Array()
    lineSubPathArray[0] = new SubPathInfo()
    lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR
    lineSubPathArray[0].closed = false
    lineSubPathArray[0].entireSubPath = lineArray


    //create the path item
    var myPathItem = document.pathItems.add("A Line", lineSubPathArray)
    // stroke it so we can see something
    // myPathItem.strokePath(ToolType.BRUSH)
    myPathItem.strokePath(ToolType.ERASER)
    document.pathItems.removeAll();
}

function eraseBlock(x1, x2, y1, y2) {
    for (var curY = startY; curY <= endY; curY += eraserSize / 3) {
        eraseLine(document, startX, endX, curY);
    }
    eraseLine(document, startX, endX, endY);
    // eraseLine(document, startX, endX, endY + eraserSize / 2);
}

function cloneStampLine(document, x1, x2, y) {
    setCloneStampPosition(x2, y - 4);
    var lineArray = new Array()
    lineArray[0] = new PathPointInfo
    lineArray[0].kind = PointKind.CORNERPOINT
    lineArray[0].anchor = Array(x2, y)

    lineArray[0].leftDirection = lineArray[0].anchor
    lineArray[0].rightDirection = lineArray[0].anchor
    lineArray[1] = new PathPointInfo
    lineArray[1].kind = PointKind.CORNERPOINT
    lineArray[1].anchor = Array(x1, y)
    lineArray[1].leftDirection = lineArray[1].anchor
    lineArray[1].rightDirection = lineArray[1].anchor
    var lineSubPathArray = new Array()
    lineSubPathArray[0] = new SubPathInfo()
    lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR
    lineSubPathArray[0].closed = false
    lineSubPathArray[0].entireSubPath = lineArray


    //create the path item
    var myPathItem = document.pathItems.add("A Line", lineSubPathArray)
    // stroke it so we can see something
    myPathItem.strokePath(ToolType.CLONESTAMP);
    document.pathItems.removeAll();
}

function cloneStampBlock(x1, x2, y1, y2) {
    for (var curY = startY; curY <= endY; curY += eraserSize / 2) {
        cloneStampLine(document, startX, endX, curY);
    }
    cloneStampLine(document, startX, endX, endY);
    // cloneStampLine(document, startX, endX, endY + eraserSize / 2);
}

function getMaskPic(markdocument, blocks) {
    markdocument.selection.selectAll();
    // 先全部涂成黑色
    markdocument.selection.fill(black);
    markdocument.selection.deselect();

    for (var i = 0; i < blocks.length; i++) {
        markdocument.selection.select(blocks[i], SelectionType.REPLACE, 0, true);
        markdocument.selection.fill(white);
        markdocument.selection.deselect();
    }
}

// 切换为仿制图章工具
function useCloneStamp() {
    (function select_79533386230469() //打开
        {
            try {
                var d = new ActionDescriptor();
                var r = new ActionReference();
                r.putClass(stringIDToTypeID("cloneStampTool"));
                d.putReference(stringIDToTypeID("null"), r);
                executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
            } catch (e) {
                if (e.number != 8007) {
                    alert("Line: " + e.line + "\n\n" + e, "Bug!", true);
                    throw (e);
                }
            }
        }
    )();
}

// 锁定当前图片
function lockPicture() {
    (function set_44743347167969() {
        try {
            var d = new ActionDescriptor();
            var r = new ActionReference();
            r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
            d.putReference(stringIDToTypeID("null"), r);
            var d1 = new ActionDescriptor();
            var d2 = new ActionDescriptor();
            d2.putBoolean(stringIDToTypeID("protectTransparency"), true);
            d1.putObject(stringIDToTypeID("layerLocking"), stringIDToTypeID("layerLocking"), d2);
            d.putObject(stringIDToTypeID("to"), stringIDToTypeID("layer"), d1);
            executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
        } catch (e) {
            if (e.number != 8007) {
                alert("Line: " + e.line + "\n\n" + e, "Bug!", true);
                throw (e);
            }
        }
    })();
}

//创建标记图像颜色
var white = new SolidColor();
white.rgb["hexValue"] = "ffffff";
var black = new SolidColor();
black.rgb["hexValue"] = "000000";

//标记图像格式
var pngSaveOption = new PNGSaveOptions();
var jpegSaveOption = new JPEGSaveOptions();
var bitmapconversionoption = new BitmapConversionOptions();

// 用于拼接文件名
var falsifyfile = "falsify";
var markfile = "mark";

// 切换为仿制图章工具
useCloneStamp();
// 设置笔触大小
setEraserSize(8)

var picturePath = "D:\\学习\\大三上\\项目\\ps脚本项目\\photoshop-script\\input\\pics\\lanxujia_ios_ipad_air3_bilibili_1.PNG"
var pictureFile = File(picturePath);
open(pictureFile);
var document = app.activeDocument;
// 锁定文件
lockPicture();

var jsonPath = "D:\\学习\\大三上\\项目\\ps脚本项目\\photoshop-script\\input\\json\\lanxujia_ios_ipad_air3_bilibili_1.PNG.json"
var jsonFile = new File(jsonPath);
if (!jsonFile.open("r")) {
    throw ("tmp file open error");
}
var fileStr = jsonFile.read();
var jsonStr = fileStrToJson(fileStr);
var arr = JSON.parse(jsonStr)

for (var i = 0; i < arr.length; i++) {
    var obj = arr[i];
    // alert(obj);
    var rect = obj['box']
    var startX = rect[0][0];
    var startY = rect[0][1];
    var endX = rect[2][0];
    var endY = rect[2][1];

    // 使用仿制图章工具进行擦除方块
    cloneStampBlock(startX, endX, startY, endY);
}